﻿using Mad.Core.DomainServices;
using Mad.Core.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mad.Infrastructure.DomainServices
{
    public class TranferService : ITranferService
    {
        private readonly IAppConfig appConfig;

        public TranferService(IAppConfig appConfig)
        {
            this.appConfig = appConfig;
        }

        public ResponseResult ConnectState(ProviderInfo provider)
        {
            SqlConnectionStringBuilder builder = appConfig.GetConnectionStringBuilder(provider);
            Database db = new SqlDatabase(builder.ConnectionString);
            DbConnection conn = db.CreateConnection();
            try
            {
                conn.Open();
                return new ResponseResult { Response = (conn.State == ConnectionState.Open) };
            }
            catch(Exception ex)
            {
                return new ResponseResult { Response =ex.Message, HasError = true };
            }
        }


        public ResponseResult MultipleBulkCopy(ProviderInfo sourceConnection, ProviderInfo destConnection, string tableName)
        {
            TableItem tableItem = new TableItem(tableName);
            SqlConnectionStringBuilder srcBuilder = appConfig.GetConnectionStringBuilder(sourceConnection);
            SqlConnectionStringBuilder destBuilder = appConfig.GetConnectionStringBuilder(destConnection);

            try
            {
                Database srcdb = new SqlDatabase(srcBuilder.ConnectionString);
                using (DbConnection srcConnect = srcdb.CreateConnection())
                {
                    srcConnect.Open();
                    SqlCommand srcCommand = new SqlCommand(string.Format(Constants.Table.QUERY_ALL_FIELDS_FROM_TABLE_SCHEMA, tableName), (SqlConnection)srcConnect);
                    srcCommand.CommandTimeout = 0;
                    SqlDataReader sqldr = srcCommand.ExecuteReader();

                    Database destdb = new SqlDatabase(destBuilder.ConnectionString);
                    using (DbConnection destConnect = destdb.CreateConnection())
                    {
                        destConnect.Open();
                        SqlCommand destCmd = new SqlCommand(string.Format(Constants.Table.QUERY_TABLE_INFORMATION_SCHEMA, tableItem.TableSchema, tableItem.TableName), (SqlConnection)destConnect);
                        SqlDataReader columns = destCmd.ExecuteReader();

                        using (SqlBulkCopy destBulkCopy = new SqlBulkCopy((SqlConnection)destConnect, SqlBulkCopyOptions.KeepIdentity | SqlBulkCopyOptions.TableLock, null))
                        {
                            destBulkCopy.BulkCopyTimeout = 0;
                            destBulkCopy.DestinationTableName = tableName;

                            while (columns.Read())
                            {
                                if (columns.GetInt32(1) != 1) destBulkCopy.ColumnMappings.Add(columns.GetString(0), columns.GetString(0));
                            }
                            columns.Close();
                            destBulkCopy.WriteToServer(sqldr);
                            sqldr.Close();

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return new ResponseResult { Response = ex.Message, HasError=true };
            }

            return new ResponseResult { Response = string.Empty };
        }
    }
}
